home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 January: Mac OS SDK / Dev.CD Jan 98 SDK1.toast / Development Kits (Disc 1) / Interfaces&Libraries / Universal / Interfaces / PInterfaces / MixedMode.p < prev    next >
Encoding:
Text File  |  1997-08-12  |  12.9 KB  |  399 lines  |  [TEXT/MPS ]

  1. {
  2.      File:        MixedMode.p
  3.  
  4.      Contains:    Mixed Mode Manager Interfaces.
  5.  
  6.      Version:    Technology:    System 7.5
  7.                  Release:    Universal Interfaces 3.0.1
  8.  
  9.      Copyright:    © 1992-1997 by Apple Computer, Inc., all rights reserved.
  10.  
  11.      Bugs?:        Please include the the file and version information (from above) with
  12.                  the problem description.  Developers belonging to one of the Apple
  13.                  developer programs can submit bug reports to:
  14.  
  15.                      devsupport@apple.com
  16.  
  17. }
  18. {$IFC UNDEFINED UsingIncludes}
  19. {$SETC UsingIncludes := 0}
  20. {$ENDC}
  21.  
  22. {$IFC NOT UsingIncludes}
  23.  UNIT MixedMode;
  24.  INTERFACE
  25. {$ENDC}
  26.  
  27. {$IFC UNDEFINED __MIXEDMODE__}
  28. {$SETC __MIXEDMODE__ := 1}
  29.  
  30. {$I+}
  31. {$SETC MixedModeIncludes := UsingIncludes}
  32. {$SETC UsingIncludes := 1}
  33.  
  34. {$IFC UNDEFINED __TYPES__}
  35. {$I Types.p}
  36. {$ENDC}
  37.  
  38.  
  39. {$PUSH}
  40. {$ALIGN MAC68K}
  41. {$LibExport+}
  42.  
  43. { Mixed Mode constants }
  44. { Current Routine Descriptor Version }
  45.  
  46. CONST
  47.     kRoutineDescriptorVersion    = 7;
  48.  
  49. { MixedModeMagic Magic Cookie/Trap number }
  50.     _MixedModeMagic                = $AAFE;
  51.  
  52. { MixedModeState Version for CFM68K Mixed Mode }
  53.     kCurrentMixedModeStateRecord = 1;
  54.  
  55. { Calling Conventions }
  56.  
  57. TYPE
  58.     CallingConventionType                = INTEGER;
  59.  
  60. CONST
  61.     kPascalStackBased            = 0;
  62.     kCStackBased                = 1;
  63.     kRegisterBased                = 2;
  64.     kD0DispatchedPascalStackBased = 8;
  65.     kD1DispatchedPascalStackBased = 12;
  66.     kD0DispatchedCStackBased    = 9;
  67.     kStackDispatchedPascalStackBased = 14;
  68.     kThinkCStackBased            = 5;
  69.  
  70. { ISA Types }
  71.  
  72. TYPE
  73.     ISAType                                = SInt8;
  74.  
  75. CONST
  76.     kM68kISA                    = 0;
  77.     kPowerPCISA                    = 1;
  78.  
  79. { RTA Types }
  80.  
  81. TYPE
  82.     RTAType                                = SInt8;
  83.  
  84. CONST
  85.     kOld68kRTA                    = $00;
  86.     kPowerPCRTA                    = $00;
  87.     kCFM68kRTA                    = $10;
  88.  
  89.  
  90. {$IFC TARGET_CPU_PPC }
  91.     GetCurrentISA                = kPowerPCISA;
  92.     GetCurrentRTA                = kPowerPCRTA;
  93. {$ELSEC}
  94.     {$IFC TARGET_CPU_68K }
  95.         GetCurrentISA            = kM68kISA;
  96.         {$IFC TARGET_RT_MAC_CFM }
  97.             GetCurrentRTA        = kCFM68kRTA;
  98.         {$ELSEC}
  99.             GetCurrentRTA        = kOld68kRTA;
  100.         {$ENDC}
  101.     {$ENDC}
  102. {$ENDC}
  103.  
  104. { Constants for specifing 68k registers }
  105.     kRegisterD0                    = 0;
  106.     kRegisterD1                    = 1;
  107.     kRegisterD2                    = 2;
  108.     kRegisterD3                    = 3;
  109.     kRegisterD4                    = 8;
  110.     kRegisterD5                    = 9;
  111.     kRegisterD6                    = 10;
  112.     kRegisterD7                    = 11;
  113.     kRegisterA0                    = 4;
  114.     kRegisterA1                    = 5;
  115.     kRegisterA2                    = 6;
  116.     kRegisterA3                    = 7;
  117.     kRegisterA4                    = 12;
  118.     kRegisterA5                    = 13;
  119.     kRegisterA6                    = 14;                            {  A7 is the same as the PowerPC SP  }
  120.     kCCRegisterCBit                = 16;
  121.     kCCRegisterVBit                = 17;
  122.     kCCRegisterZBit                = 18;
  123.     kCCRegisterNBit                = 19;
  124.     kCCRegisterXBit                = 20;
  125.  
  126.  
  127. TYPE
  128.     registerSelectorType                = INTEGER;
  129. { SizeCodes we use everywhere }
  130.  
  131. CONST
  132.     kNoByteCode                    = 0;
  133.     kOneByteCode                = 1;
  134.     kTwoByteCode                = 2;
  135.     kFourByteCode                = 3;
  136.  
  137. { Mixed Mode Routine Records }
  138.  
  139. TYPE
  140.     ProcInfoType                        = LONGINT;
  141. { Routine Flag Bits }
  142.     RoutineFlagsType                    = INTEGER;
  143.  
  144. CONST
  145.     kProcDescriptorIsAbsolute    = $00;
  146.     kProcDescriptorIsRelative    = $01;
  147.  
  148.     kFragmentIsPrepared            = $00;
  149.     kFragmentNeedsPreparing        = $02;
  150.  
  151.     kUseCurrentISA                = $00;
  152.     kUseNativeISA                = $04;
  153.  
  154.     kPassSelector                = $00;
  155.     kDontPassSelector            = $08;
  156.  
  157.     kRoutineIsNotDispatchedDefaultRoutine = $00;
  158.     kRoutineIsDispatchedDefaultRoutine = $10;
  159.  
  160.     kProcDescriptorIsProcPtr    = $00;
  161.     kProcDescriptorIsIndex        = $20;
  162.  
  163.  
  164. TYPE
  165.     RoutineRecordPtr = ^RoutineRecord;
  166.     RoutineRecord = RECORD
  167.         procInfo:                ProcInfoType;                            {  calling conventions  }
  168.         reserved1:                SInt8;                                    {  Must be 0  }
  169.         ISA:                    ISAType;                                {  Instruction Set Architecture  }
  170.         routineFlags:            RoutineFlagsType;                        {  Flags for each routine  }
  171.         procDescriptor:            ProcPtr;                                {  Where is the thing we’re calling?  }
  172.         reserved2:                UInt32;                                    {  Must be 0  }
  173.         selector:                UInt32;                                    {  For dispatched routines, the selector  }
  174.     END;
  175.  
  176.     RoutineRecordHandle                    = ^RoutineRecordPtr;
  177. { Mixed Mode Routine Descriptors }
  178. { Definitions of the Routine Descriptor Flag Bits }
  179.     RDFlagsType                            = UInt8;
  180.  
  181. CONST
  182.     kSelectorsAreNotIndexable    = $00;
  183.     kSelectorsAreIndexable        = $01;
  184.  
  185. { Routine Descriptor Structure }
  186.  
  187. TYPE
  188.     RoutineDescriptorPtr = ^RoutineDescriptor;
  189.     RoutineDescriptor = PACKED RECORD
  190.         goMixedModeTrap:        UInt16;                                    {  Our A-Trap  }
  191.         version:                SInt8;                                    {  Current Routine Descriptor version  }
  192.         routineDescriptorFlags:    RDFlagsType;                            {  Routine Descriptor Flags  }
  193.         reserved1:                UInt32;                                    {  Unused, must be zero  }
  194.         reserved2:                UInt8;                                    {  Unused, must be zero  }
  195.         selectorInfo:            UInt8;                                    {  If a dispatched routine, calling convention, else 0  }
  196.         routineCount:            UInt16;                                    {  Number of routines in this RD  }
  197.         routineRecords:            ARRAY [0..0] OF RoutineRecord;            {  The individual routines  }
  198.     END;
  199.  
  200.     RoutineDescriptorHandle                = ^RoutineDescriptorPtr;
  201. { 68K MixedModeStateRecord }
  202.     MixedModeStateRecordPtr = ^MixedModeStateRecord;
  203.     MixedModeStateRecord = RECORD
  204.         state1:                    UInt32;
  205.         state2:                    UInt32;
  206.         state3:                    UInt32;
  207.         state4:                    UInt32;
  208.     END;
  209.  
  210. { Mixed Mode ProcInfos }
  211.  
  212. CONST
  213.                                                                 {  Calling Convention Offsets  }
  214.     kCallingConventionWidth        = 4;
  215.     kCallingConventionPhase        = 0;
  216.     kCallingConventionMask        = $0F;                            {  Result Offsets  }
  217.     kResultSizeWidth            = 2;
  218.     kResultSizePhase            = 4;
  219.     kResultSizeMask                = $30;                            {  Parameter offsets & widths  }
  220.     kStackParameterWidth        = 2;
  221.     kStackParameterPhase        = 6;
  222.     kStackParameterMask            = $FFFFFFC0;                    {  Register Result Location offsets & widths  }
  223.     kRegisterResultLocationWidth = 5;
  224.     kRegisterResultLocationPhase = 6;                            {  Register Parameter offsets & widths  }
  225.     kRegisterParameterWidth        = 5;
  226.     kRegisterParameterPhase        = 11;
  227.     kRegisterParameterMask        = $7FFFF800;
  228.     kRegisterParameterSizePhase    = 0;
  229.     kRegisterParameterSizeWidth    = 2;
  230.     kRegisterParameterWhichPhase = 2;
  231.     kRegisterParameterWhichWidth = 3;                            {  Dispatched Stack Routine Selector offsets & widths  }
  232.     kDispatchedSelectorSizeWidth = 2;
  233.     kDispatchedSelectorSizePhase = 6;                            {  Dispatched Stack Routine Parameter offsets  }
  234.     kDispatchedParameterPhase    = 8;                            {  Special Case offsets & widths  }
  235.     kSpecialCaseSelectorWidth    = 6;
  236.     kSpecialCaseSelectorPhase    = 4;
  237.     kSpecialCaseSelectorMask    = $03F0;
  238.  
  239.     kSpecialCase                = $000F;                        {  (CallingConventionType)  }
  240.  
  241.                                                                 {  all of the special cases enumerated.  The selector field is 6 bits wide  }
  242.     kSpecialCaseHighHook        = 0;
  243.     kSpecialCaseCaretHook        = 0;                            {  same as kSpecialCaseHighHook  }
  244.     kSpecialCaseEOLHook            = 1;
  245.     kSpecialCaseWidthHook        = 2;
  246.     kSpecialCaseTextWidthHook    = 2;                            {  same as kSpecialCaseWidthHook  }
  247.     kSpecialCaseNWidthHook        = 3;
  248.     kSpecialCaseDrawHook        = 4;
  249.     kSpecialCaseHitTestHook        = 5;
  250.     kSpecialCaseTEFindWord        = 6;
  251.     kSpecialCaseProtocolHandler    = 7;
  252.     kSpecialCaseSocketListener    = 8;
  253.     kSpecialCaseTERecalc        = 9;
  254.     kSpecialCaseTEDoText        = 10;
  255.     kSpecialCaseGNEFilterProc    = 11;
  256.     kSpecialCaseMBarHook        = 12;
  257.  
  258.  
  259. {
  260.     NOTES ON USING ROUTINE DESCRIPTOR FUNCTIONS
  261.     
  262.     When calling these routine from classic 68k code there are two possible intentions.
  263.  
  264.     The first is source compatibility with code ported to CFM (either PowerPC or 68k CFM). When
  265.     the code is compiled for CFM the functions create routine descriptors that can be used by
  266.     the mixed mode manager operating on that machine. When the code is compiled for classic 68k
  267.     these functions do nothing so that the code will run on Macintoshes that do not have a
  268.     mixed mode manager. The dual nature of these functions is achieved by turning the CFM calls
  269.     into "no-op" macros for classic 68k: You can put "NewRoutineDescriptor" in your source,
  270.     compile it for any runtime or instruction set architecture, and it will run correctly on the
  271.     intended runtime/instruction platform. All without source changes and/or conditional source.
  272.     
  273.     The other intention is for code that "knows" that it is executing as classic 68k runtime
  274.     and is specifically trying to call code of another architecture using mixed mode. Since the
  275.     routines were designed with classic <-> CFM source compatibility in mind this second case
  276.     is treated special. For classic 68k code to create routines descriptors for use by mixed mode
  277.     it must call the "Trap" versions of the routines (NewRoutineDescriptorTrap). These versions
  278.     are only available to classic 68k callers: rigging the interfaces to allow calling them
  279.     from CFM code will result in runtime failure because no shared library implements or exports
  280.     the functions.
  281.     
  282.  
  283.     This almost appears seamless until you consider "fat" routine descriptors and the advent of
  284.     CFM-68K. What does "fat" mean? CFM-68K is not emulated on PowerPC and PowerPC is not emulated
  285.     on CFM-68K. It makes no sense to create a routine descriptor having both a CFM-68K routine
  286.     and a PowerPC native routine pointer. Therefore "fat" is defined to be a mix of classic and
  287.     CFM for the hardware's native instruction set: on PowerPC fat is classic and PowerPC native,
  288.     on a 68k machine with CFM-68K installed fat is classic and CFM-68K.
  289.     
  290.     By definition fat routine descriptors are only constructed by code that is aware of the 
  291.     architecture it is executing as and that another architecture exists. Source compatibility
  292.     between code intented as pure classic and pure CFM is not an issue and so NewFatRoutineDescriptor
  293.     is not available when building pure classic code.
  294.     
  295.     NewFatRoutineDescriptorTrap is available to classic code on both PowerPC and CFM-68K. The
  296.     classic code can use the code fragment manager routine "FindSymbol" to obtain the address of 
  297.     a routine in a shared library and then construct a routine descriptor with both the CFM routine 
  298.     and classic    routine.
  299. }
  300.  
  301. {$IFC TARGET_OS_MAC AND TARGET_RT_MAC_CFM }
  302. FUNCTION NewRoutineDescriptor(theProc: ProcPtr; theProcInfo: ProcInfoType; theISA: ISAType): UniversalProcPtr;
  303. PROCEDURE DisposeRoutineDescriptor(theProcPtr: UniversalProcPtr);
  304. FUNCTION NewFatRoutineDescriptor(theM68kProc: ProcPtr; thePowerPCProc: ProcPtr; theProcInfo: ProcInfoType): UniversalProcPtr;
  305. {$ELSEC}
  306. { Note that the call to NewFatRoutineDescriptor is undefined. }
  307. FUNCTION NewRoutineDescriptor(theProc: ProcPtr; theProcInfo: ProcInfoType; theISA: ISAType): UniversalProcPtr;
  308.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  309.     INLINE $5C4F, $2E9F;
  310.     {$ENDC}
  311. PROCEDURE DisposeRoutineDescriptor(theProcPtr: UniversalProcPtr);
  312.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  313.     INLINE $584F;
  314.     {$ENDC}
  315. {$ENDC}
  316.  
  317.  
  318. {$IFC TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM }
  319. {
  320.     The "Trap" versions of the MixedMode calls are only for classic 68K clients that 
  321.     want to load and use CFM based code.
  322. }
  323. FUNCTION NewRoutineDescriptorTrap(theProc: ProcPtr; theProcInfo: ProcInfoType; theISA: ISAType): UniversalProcPtr;
  324.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  325.     INLINE $7000, $AA59;
  326.     {$ENDC}
  327. PROCEDURE DisposeRoutineDescriptorTrap(theProcPtr: UniversalProcPtr);
  328.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  329.     INLINE $7001, $AA59;
  330.     {$ENDC}
  331. FUNCTION NewFatRoutineDescriptorTrap(theM68kProc: ProcPtr; thePowerPCProc: ProcPtr; theProcInfo: ProcInfoType): UniversalProcPtr;
  332.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  333.     INLINE $7002, $AA59;
  334.     {$ENDC}
  335. {$ENDC}
  336.  
  337. {$IFC TARGET_RT_MAC_CFM }
  338. {
  339.     CallUniversalProc is only implemented in shared libraries on CFM68K and PowerPC, it is now
  340.     conditionalize with TARGET_RT_MAC_CFM.  This will catch accidental calls from classic 68K code
  341.     that previously only showed up as linker errors.
  342. }
  343. FUNCTION CallUniversalProc(theProcPtr: UniversalProcPtr; procInfo: ProcInfoType; ...): LONGINT; C;
  344. FUNCTION CallOSTrapUniversalProc(theProcPtr: UniversalProcPtr; procInfo: ProcInfoType; ...): LONGINT; C;
  345. FUNCTION CallAsyncUniversalProc(theProcPtr: UniversalProcPtr; procInfo: ProcInfoType; ...): LONGINT; C;
  346. {$ENDC}  {TARGET_RT_MAC_CFM}
  347.  
  348. {$IFC TARGET_CPU_68K }
  349. FUNCTION SaveMixedModeState(VAR stateStorage: MixedModeStateRecord; stateVersion: UInt32): OSErr;
  350.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  351.     INLINE $7003, $AA59;
  352.     {$ENDC}
  353. FUNCTION RestoreMixedModeState(VAR stateStorage: MixedModeStateRecord; stateVersion: UInt32): OSErr;
  354.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  355.     INLINE $7004, $AA59;
  356.     {$ENDC}
  357. {$ENDC}  {TARGET_CPU_68K}
  358.  
  359. { * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
  360.  *
  361.  *    Macros for building ProcInfos.  Examples:
  362.  *    
  363.  *    
  364.  *    uppModalFilterProcInfo = kPascalStackBased
  365.  *         | RESULT_SIZE(SIZE_CODE(sizeof(Boolean)))
  366.  *         | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(DialogPtr)))
  367.  *         | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(EventRecord*)))
  368.  *         | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(short*))),
  369.  *
  370.  *    uppDeskHookProcInfo = kRegisterBased
  371.  *         | REGISTER_ROUTINE_PARAMETER(1, kRegisterD0, SIZE_CODE(sizeof(Boolean)))
  372.  *         | REGISTER_ROUTINE_PARAMETER(2, kRegisterA0, SIZE_CODE(sizeof(EventRecord*)))
  373.  *
  374.  *    uppGXSpoolResourceProcInfo = kCStackBased
  375.  *         | RESULT_SIZE(SIZE_CODE(sizeof(OSErr)))
  376.  *         | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(gxSpoolFile)))
  377.  *         | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(Handle)))
  378.  *         | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(ResType)))
  379.  *         | STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof(long)))
  380.  *
  381.  *    uppTEFindWordProcInfo = SPECIAL_CASE_PROCINFO( 6 ),
  382.  *
  383.  }
  384.  
  385.  
  386.  
  387.  
  388.  
  389. {$ALIGN RESET}
  390. {$POP}
  391.  
  392. {$SETC UsingIncludes := MixedModeIncludes}
  393.  
  394. {$ENDC} {__MIXEDMODE__}
  395.  
  396. {$IFC NOT UsingIncludes}
  397.  END.
  398. {$ENDC}
  399.